﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompanyBussiness
{
    public class AccountManagement : IAccountManagement
    {
        public string _loginUser = null;
        //CompanyDataContext companydata = new CompanyDataContext();
        CompanyDataContext companydata = new CompanyDataContext();

        public string LoginUser(string userName, string password)
        {          
            var user = from ac in companydata.Accounts
                      where ac.UserName == userName && ac.Password == password && ac.AccountStatus==true
                      select ac;

            if (user.Count<Account>() == 0)
            {
                _loginUser = null;
            }
            else
                _loginUser = userName;

            return _loginUser;
        }

        public virtual bool CheckPermissionOfAccount(Account acc)
        {
            bool result=false;
            var internalUser = (from p in companydata.VwPermissionUsers
                                where p.AccountID == acc.AccountID
                                select p);
            if (internalUser.Count() > 1)
                result= true;
            else //if (internalUser.Single().ModulCode == "MSHAREHOLDERSFONTEND")
            {
                result= false;
            }

            return result;
        }

        public virtual Account GetAccountByUserName(string userName)
        {
            var user = (from ac in companydata.Accounts
                       where ac.UserName == userName
                       select ac).FirstOrDefault();
            return user;
        }

        public string ForgotPassword(string userName, string email)
        {
            var user = from ac in companydata.Accounts
                       where ac.UserName == userName && ac.Email == email
                       select ac;
            if (user.Count<Account>() == 0)
            {
                _loginUser = null;
            }
            else
                _loginUser = userName;

           

            return _loginUser;
        }

        public void LogOut()
        {
            _loginUser = null;
        }

        public void CreateUser(string userName, string password, string fName, string lName, string email, string nick, string address, string phone, Boolean gender, string images)
        {
            Account ac = new Account();
            ac.UserName = userName;
            ac.Password = password;
            ac.FirstName = fName;
            ac.LastName = lName;
            ac.Email = email;
            ac.Nick = nick;
            ac.Address = address;
            ac.Phone = phone;
            ac.Gender = gender;
            ac.Images = images;
            ac.Roles = true;
            ac.AccountStatus = true;

            companydata.Accounts.InsertOnSubmit(ac);
            companydata.SubmitChanges();

        }


        public void ChangePassword(string userName, string password)
        {
            var user = (from ac in companydata.Accounts
                        where ac.UserName == userName
                        select ac).Single();
            user.Password = password;

            companydata.SubmitChanges();
        }

        public void DeleteAccount(int accountID)
        {
            var user = (from ac in companydata.Accounts
                        where ac.AccountID == accountID
                        select ac).Single();

            companydata.Accounts.DeleteOnSubmit(user);
            companydata.SubmitChanges();
        }

        public void UpdateAccount(int accountID, string userName, string password, string fName, string lName, string email, string nick, string address, string phone, Boolean gender, string images, Boolean roles)
        {
            var user = (from ac in companydata.Accounts
                        where ac.AccountID == accountID
                        select ac).Single();

            user.UserName = userName;
            user.Password = password;
            user.FirstName = fName;
            user.LastName = lName;
            user.Email = email;
            user.Nick = nick;
            user.Address = address;
            user.Phone = phone;
            user.Gender = gender;
            user.Images = images;

            companydata.SubmitChanges();
        }



        public virtual AccountLog GetSystemLog(int accountID)
        {
            var log = (from lo in companydata.AccountLogs
                       where lo.AccountID == accountID
                       select lo).FirstOrDefault();
            return log;
        }
        public virtual VwLogUser GetLogByUserName(string userName)
        {
            var log = (from v in companydata.VwLogUsers
                       where v.UserName == userName
                       select v);

            var logaccount = (from lg in log
                              orderby lg.AccountLogID descending
                              select lg);
            return logaccount.Take(1).Single();
        }
        public virtual string GetSystemLogAll()
        {
            var log = (from lo in companydata.AccountLogs
                       select lo).ToString();

            return log;
        }
        public virtual VwLogUser GetLastLog(string userName)
        {
            var log = (from v in companydata.VwLogUsers
                       where v.UserName == userName
                       select v);

            var logaccount = (from lg in log
                              orderby lg.AccountLogID descending
                              select lg);
            if (logaccount.LongCount() > 1)
            {
                var loglast = (from lg in logaccount
                               select lg).FirstOrDefault();
                return loglast;
            }
            else
            {
                return logaccount.Single();
            }

        }
        public virtual void CreateLog(DateTime accountLogTime, string accountLogDetail, string IPAddress, int accountID)
        {
            AccountLog accountLog = new AccountLog();
            accountLog.AccountLogTime = accountLogTime;
            accountLog.AccountLogDetail = accountLogDetail;
            accountLog.IPAddress = IPAddress;
            accountLog.AccountID = accountID;

            companydata.AccountLogs.InsertOnSubmit(accountLog);
            companydata.SubmitChanges();
        }

        public virtual void UpdateLog(int accountLogID, string accountLogDetail, int accountID)
        {
            var log = (from lo in companydata.AccountLogs
                       where lo.AccountID == accountID
                       select lo).Single();
            log.AccountLogDetail = log.AccountLogDetail + accountLogDetail;
            log.AccountID = accountID;

            companydata.SubmitChanges();
        }

        public virtual void CreateInternalUser(InternalUser internerUser)
        {
            internerUser.DateCreate = DateTime.Now;
            companydata.InternalUsers.InsertOnSubmit(internerUser);
            companydata.SubmitChanges();

        }

        public virtual Modul GetModulByModulCode(string modulCode)
        {
            var modul = (from m in companydata.Moduls
                         where m.ModulCode == modulCode
                         select m).FirstOrDefault();
            return modul;
        }

        public virtual InternalUser GetInternalByUserName(string userName)
        {
            var internaluser = (from m in companydata.InternalUsers
                                join p in companydata.Accounts on m.AccountID equals p.AccountID
                                where p.UserName == userName
                                select m).FirstOrDefault();

            return internaluser;
        }

        public virtual void SetPermission(InternalUser internerUser, Modul modul)
        {
            Permission per = new Permission();
            //per.PermissionName = modul.ModulName;
            per.ModulID = modul.ModulID;
            per.InternalID = internerUser.InternalID;

            companydata.Permissions.InsertOnSubmit(per);
            companydata.SubmitChanges();
        }

        public virtual int GetPermissionByAllID(InternalUser internerUser, Modul modul)
        {
            var perUser = (from p in companydata.Permissions
                           where p.InternalID == internerUser.InternalID && p.ModulID == modul.ModulID
                           select p);
            return perUser.Count();
        }

        public virtual void DeletePermissionByAllID(InternalUser internerUser, Modul modul)
        {
            //Permission per = new Permission();
            ////per.PermissionName = modul.ModulName;
            //per.ModulID = modul.ModulID;
            //per.InternalID = internerUser.InternalID;

            var per = (from p in companydata.Permissions
                       where p.ModulID == modul.ModulID && p.InternalID == internerUser.InternalID
                       select p).Single();

            companydata.Permissions.DeleteOnSubmit(per);
            companydata.SubmitChanges();
        }

        public virtual void ActiveAccount(Account account)
        {
            var acc = (from a in companydata.Accounts
                       where a.AccountID == account.AccountID
                       select a).Single();
            acc.AccountStatus = true;

            companydata.SubmitChanges();
        }

        public virtual int GetAccountIDByInternalUserID(int internalID)
        {
            var inter = (from a in companydata.InternalUsers
                         where a.InternalID == internalID
                         select a).FirstOrDefault();
            return (int)inter.AccountID;
        }

        public virtual List<VwInternalUser> GetAccountInternalSearch(VwInternalUser vw, DateTime DateForm, DateTime DateTo, int take, int StartRow)
        {
            var sInternal = (from ne in companydata.VwInternalUsers
                         select ne);
            if (vw.UserName != String.Empty)
            {
                sInternal = (from n in sInternal
                             where n.UserName.Equals(vw.UserName) == true
                             select n);
            }

            if (vw.Status != null)
            {
                sInternal = (from ne in sInternal
                         where ne.AccountStatus == vw.Roles
                         select ne);
            }

            sInternal = (from p in sInternal
                         where p.DateCreate >= DateForm && p.DateCreate <= DateTo
                         orderby p.DateCreate descending
                     select p);

            if (sInternal.Count() < take)
                return sInternal.ToList();
            else
                return sInternal.Skip(StartRow).Take(take).ToList();

        }

        public virtual int GetNumberInternalSearch(VwInternalUser vw, DateTime DateForm, DateTime DateTo)
        {
            var sInternal = (from ne in companydata.VwInternalUsers
                             select ne);
            if (vw.UserName != String.Empty)
            {
                sInternal = (from n in sInternal
                             where n.UserName.Equals(vw.UserName) == true
                             select n);
            }

            if (vw.Status != null)
            {
                sInternal = (from ne in sInternal
                             where ne.AccountStatus == vw.Roles
                             select ne);
            }

            sInternal = (from p in sInternal
                         where p.DateCreate >= DateForm && p.DateCreate <= DateTo
                         orderby p.DateCreate descending
                         select p);
            
            return sInternal.Count()!=0 ? sInternal.Count() :0;
        }

        public virtual List<VwPermissionUser> GetPermissionUserByAccountID(int accountID)
        {
            var perUser = (from p in companydata.VwPermissionUsers
                           where p.AccountID == accountID
                           select p);

            return perUser.ToList();
        }

        public virtual Modul GetModulByID(int modulID)
        {
            var modul = (from p in companydata.Moduls
                         where p.ModulID == modulID
                         select p).FirstOrDefault();
            return modul;
        }

        public virtual VwPermissionUser GetVwPerUserByAccountID(int accountID)
        {
            var perUser = (from p in companydata.VwPermissionUsers
                           where p.AccountID == accountID
                           select p).FirstOrDefault();

            return perUser;
        }

        public virtual VwInternalUser GetViewInternalUserByAccountID(int accountID)
        {
            var intUser = (from p in companydata.VwInternalUsers
                           where p.AccountID == accountID
                           select p).FirstOrDefault();
            return intUser;
        }

        public virtual int CheckEmail(string email)
        {
            var internalUser = (from p in companydata.InternalUsers
                                where p.Email == email
                                select p);
            return internalUser.Count();
        }


        public virtual void UpdateInternalUserByAccoutID(int accountID,string email,string fullName,string address,string phone)
        {
            var internalUser = (from p in companydata.InternalUsers
                                where p.AccountID == accountID
                                select p).FirstOrDefault();

            internalUser.Email = email;
            internalUser.FullName = fullName;
            internalUser.Address = address;
            internalUser.Phone = phone;

            var acc = (from p in companydata.Accounts
                       where p.AccountID == accountID
                       select p).Single();

            acc.Email = email;

            companydata.SubmitChanges();
        }


        public virtual int CreateAccount(Account acc)
        {
            companydata.Accounts.InsertOnSubmit(acc);
            companydata.SubmitChanges();

            return acc.AccountID;
        }

        public virtual List<Modul> GetListModul()
        {
            var modul = (from p in companydata.Moduls
                         select p).ToList();
            return modul;
        }

    }
}
